package com.mapbased.sfw.binding;

import com.mapbased.sfw.component.Component;
import com.mapbased.sfw.component.XmlContext;
import com.mapbased.sfw.model.LoadContext;
import com.mapbased.sfw.model.RenderContext;
import com.mapbased.sfw.model.LoadContext.PaserException;

public class Comp extends Binding {

	///private final Object[] vars;
	private int index;
	private String backup;
	public Comp(String cmd,LoadContext lc){
		int p=cmd.indexOf(".");
		if(p<=0){
			throw new PaserException("Comp binding master have a '.' to seperate component and var:"+cmd);
		}
		String comp=cmd.substring(0,p);
		String var=cmd.substring(p+1);
		Component component=lc.getComponents().get(comp);
		if(component==null){
			throw new PaserException("cannot find a component named:"+comp+"  while parsing:"+cmd);

		}
		int index=component.mapVar(var);
		if(index<=0){
			throw new PaserException("cannot find a var called:"+var+" while parsing:"+cmd);

		}
		this.index=index;
	//	this.vars=component.getVars();
		this.backup=cmd;
	 
	}
	 
	@Override
	public Object getValue(RenderContext rc) {
		 return rc.getVar(index);
		//return vars[index];
	}

	@Override
	public void toXml(XmlContext xc) {
		 xc.sb.append("@comp{").append(backup).append("}");

	}

}
